﻿package net.wangit.adm.org;

import net.wangit.MapList;
import net.wangit.Row;
import net.wangit.context.ActionContext;
import net.wangit.jdbc.ColumnRow;
import net.wangit.jdbc.DB;
import net.wangit.jdbc.Table;
import net.wangit.support.action.DefaultAction;
import net.wangit.user.admin.AdminCache;
import net.wangit.user.org.OrgCache;
import net.wangit.util.TreeUtil;

public class SaveOrgAction extends DefaultAction {

	private static final String ORGS_UPDATE = "update AORG set parentid=? where parentid=?";
	private static final String ADMINISTRATOR_UPDATE = "update AADMINORG set orgid=? where orgid=?";
	private static final String ROLE_UPDATE = "update AORGROLE set orgid=? where orgid=?";
	private static final String USER_UPDATE = "update AUSER set orgid=? where orgid=?";
	private static final String LEVEL_QUERY = "select * from AORG";
	private static final String LEVEL_UPDATE = "update AORG set orglevel=? where orgid=?";
	private static final int LEVEL_UPDATE_TYPES[] = { 11, 2 };
	private static final String LEVEL_DELETE = "delete from AORGLEVEL";
	private static final String LEVEL_INSERT = "insert into AORGLEVEL (upperid,lowerid) values (?,?)";
	private static final int LEVEL_INSERT_TYPES[] = { 2, 2 };

	public SaveOrgAction() {
	}

	public void doAction(DB db, ActionContext ac) throws Exception {
		super.doAction(db, ac);
		ColumnRow row = (ColumnRow) ((Table) ac.getTables().get(0)).getRows()
				.get(0);
		if (row.isUpdateRow()) {
			String newId = row.get("orgid");
			String oldId = row.getPrimaryKey("orgid");
			update(db, newId, oldId);
		}
		MapList orgs = db.query("select * from AORG");
		updateLevel(db, orgs);
		insertLevel(db, orgs);
		if (row.isUpdateRow()) {
			OrgCache.clearAll();
			AdminCache.clearAll();
		}
	}

	private void update(DB db, String newId, String oldId) throws Exception {
		if (!oldId.equals(newId)) {
			String values[] = { newId, oldId };
			int types[] = { 2, 2 };
			db.execute("update AORG set parentid=? where parentid=?", values,
					types);
			db.execute("update AADMINORG set orgid=? where orgid=?", values,
					types);
			db.execute("update AORGROLE set orgid=? where orgid=?", values,
					types);
			db.execute("update AUSER set orgid=? where orgid=?", values, types);
		}
	}

	private void updateLevel(DB db, MapList orgs) throws Exception {
		for (int i = 0; i < orgs.size(); i++) {
			String orgId = orgs.getRow(i).get("orgid");
			MapList parents = TreeUtil.getParents(orgs, orgId, "orgid",
					"parentid");
			db.execute("update AORG set orglevel=? where orgid=?",
					new String[] { Integer.toString(parents.size()), orgId },
					LEVEL_UPDATE_TYPES);
		}

	}

	private void insertLevel(DB db, MapList orgs) throws Exception {
		db.execute("delete from AORGLEVEL");
		for (int i = 0; i < orgs.size(); i++) {
			String upperId = orgs.getRow(i).get("orgid");
			insertLevel(db, orgs, upperId, upperId);
		}

	}

	private void insertLevel(DB db, MapList orgs, String upperId,
			String parentId) throws Exception {
		for (int i = 0; i < orgs.size(); i++) {
			Row org = orgs.getRow(i);
			if (parentId.equals(org.get("parentid"))) {
				String childId = org.get("orgid");
				db.execute(
						"insert into AORGLEVEL (upperid,lowerid) values (?,?)",
						new String[] { upperId, childId }, LEVEL_INSERT_TYPES);
				insertLevel(db, orgs, upperId, childId);
			}
		}

	}

}
